﻿using JinfuyuanProject.Entity;
using JinfuyuanProject.Repository.Interface;
using JinfuyuanProject.Service.Interface;
using System;
using System.Collections;
using System.Linq;

namespace JinfuyuanProject.Service
{
    public class ServicesService : IServicesService
    {
        private readonly IServicesRepository _servicesRepository;

        public ServicesService(IServicesRepository servicesRepository)
        {
            _servicesRepository = servicesRepository;
        }
        #region 基本方法
        public bool Delete(Services services)
        {
            return _servicesRepository.Delete(services);
        }

        public IQueryable<Services> GetList()
        {
            return _servicesRepository.GetList();
        }

        public Services GetModel(int Id)
        {
            return _servicesRepository.GetModel(Id);
        }

        public int Insert(Services services)
        {
            return _servicesRepository.Insert(services);
        }
        
        public bool Update(Services services)
        {
            return _servicesRepository.Update(services);
        }
        #endregion
        JinfuyuanDbContext db = new JinfuyuanDbContext();
        #region 业务方法
        public Hashtable ListData(ServicesFilter filter)  //服务表
        {
            int count;
            IQueryable<Services> serviceList = GetList();
            // 添加服务名称搜索条件
            if (!string.IsNullOrEmpty(filter.ServiceName))     //IsNullOrEmpty检查字符串是否为 null 
            {
                serviceList = serviceList.Where(x => x.ServiceName.Contains(filter.ServiceName));
            }
            Hashtable hashtable = new Hashtable();
            hashtable["code"] = 0;
            hashtable["message"] = "";
            hashtable["data"] = MakeQueryFilter(serviceList,filter,out count);
            hashtable["count"] = count;
            return hashtable;
        }

        public Hashtable InsertData(Services service)
        {
            Hashtable hashtable = new Hashtable();
            try
            {
                // 调试：检查接收到的数据
                System.Diagnostics.Debug.WriteLine($"接收到的服务名称: {service?.ServiceName}");

                // 加强空值检查
                if (service == null || string.IsNullOrWhiteSpace(service.ServiceName))
                {
                    hashtable["code"] = 1;
                    hashtable["msg"] = "服务名称不能为空";
                    return hashtable;
                }

                // 确保其他必填字段也有值
                if (service.ServicePrice <= 0)
                {
                    hashtable["code"] = 1;
                    hashtable["msg"] = "服务价格必须大于0";
                    return hashtable;
                }

                // 设置默认值
                service.IsDelete = 0;
                service.CreateTime = DateTime.Now;

                int result = _servicesRepository.Insert(service);

                if (result > 0)
                {
                    hashtable["code"] = 0;
                    hashtable["msg"] = "添加成功";
                }
                else
                {
                    hashtable["code"] = 1;
                    hashtable["msg"] = "添加失败";
                }
            }
            catch (Exception ex)
            {
                hashtable["code"] = 1;
                hashtable["msg"] = $"添加时发生错误：{ex.Message}";
            }

            return hashtable;
        }

        public Hashtable DeleteData(int id)   //物理删除
        {
            Hashtable hashtable = new Hashtable();
            try
            {
                // 获取要删除的服务
                Services service = _servicesRepository.GetModel(id);
                if (service == null)
                {
                    hashtable["code"] = 1;
                    hashtable["msg"] = "未找到该服务信息";
                    return hashtable;
                }

                // 执行物理删除
                bool result = _servicesRepository.Delete(service);

                if (result)
                {
                    hashtable["code"] = 0;
                    hashtable["msg"] = "删除成功";
                }
                else
                {
                    hashtable["code"] = 1;
                    hashtable["msg"] = "删除失败";
                }
            }
            catch (Exception ex)
            {
                hashtable["code"] = 1;
                hashtable["msg"] = "删除时发生错误：" + ex.Message;
            }

            return hashtable;
        }

        public Hashtable EditData(Services service)
        {
            Hashtable hashtable = new Hashtable();
            try
            {
                // 验证必填字段
                if (string.IsNullOrEmpty(service.ServiceName)) //IsNullOrEmpty检查字符串是否为 null 
                {
                    hashtable["code"] = 1;
                    hashtable["msg"] = "服务名称不能为空";
                    return hashtable;
                }

                if (service.ServicePrice <= 0)
                {
                    hashtable["code"] = 1;
                    hashtable["msg"] = "服务价格必须大于0";
                    return hashtable;
                }

                // 获取现有服务数据
                Services model = _servicesRepository.GetModel(service.Id);
                if (model == null)
                {
                    hashtable["code"] = 1;
                    hashtable["msg"] = "未找到该服务信息";
                    return hashtable;
                }

                // 更新字段
                model.ServiceName = service.ServiceName;
                model.ServicePrice = service.ServicePrice;
                model.SerStartTime = service.SerStartTime;
                model.ServiceInterval = service.ServiceInterval;
                model.ServiceThreshold = service.ServiceThreshold;
                model.ServiceType = service.ServiceType;
                model.IsDelete = service.IsDelete;
                model.ServiceCount = service.ServiceCount;

                // 保留原始创建时间，不更新CreateTime字段
                // model.CreateTime = service.CreateTime;

                // 调用基础Update方法
                bool result = _servicesRepository.Update(model);

                if (result)
                {
                    hashtable["code"] = 0;
                    hashtable["msg"] = "更新成功";
                    hashtable["data"] = model;
                }
                else
                {
                    hashtable["code"] = 1;
                    hashtable["msg"] = "更新失败";
                }
            }
            catch (Exception ex)
            {
                hashtable["code"] = 1;
                hashtable["msg"] = "更新时发生错误：" + ex.Message;
            }

            return hashtable;
        }  //编辑更新

        public Hashtable GetServiceById(int id)  //编辑时得到传入Id
        {
            Hashtable hashtable = new Hashtable();
            try
            {
                Services service = _servicesRepository.GetModel(id);
                if (service == null)
                {
                    hashtable["code"] = 1;
                    hashtable["msg"] = "未找到该服务信息";
                    return hashtable;
                }

                hashtable["code"] = 0;
                hashtable["msg"] = "获取成功";
                hashtable["data"] = service;
            }
            catch (Exception ex)
            {
                hashtable["code"] = 1;
                hashtable["msg"] = "获取服务信息时出错：" + ex.Message;
            }

            return hashtable;
        }

        #endregion

        #region 公共方法
        public IQueryable<Services> MakeQueryFilter(IQueryable<Services> services, ServicesFilter servicesFilter,out int count)  //分页功能
        {
            count = services.Count();
            if (servicesFilter.page != 0 && servicesFilter.limit != 0)
                return services.OrderBy(x => x.Id).Skip((servicesFilter.page - 1) * servicesFilter.limit).Take(servicesFilter.limit);
            else
                return services;
        }
        #endregion

    }
}
